package com.doctor.windflower_doctor.fragment;

import java.lang.reflect.Field;

import com.umeng.analytics.MobclickAgent;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * <p>
 * 功能 fragment 封装类
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年9月25日 上午11:52:15
 * @version 1.0 </br> 最后修改人 无
 */
public abstract class BaseFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (savedInstanceState != null) {
			getSaveData(savedInstanceState);
		}
		View view = setConentView(inflater, container, savedInstanceState);
		findViews(view);
		setListener();
		processLogic();
		return view;
	}

	protected abstract View setConentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

	protected abstract void getSaveData(Bundle savedInstanceState);

	protected abstract void processLogic();

	protected abstract void setListener();

	protected abstract void findViews(View view);

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onDetach() {
		super.onDetach();
		try {
			// 参数是固定写法
			Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
			childFragmentManager.setAccessible(true);
			childFragmentManager.set(this, null);
		} catch (NoSuchFieldException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart("MainScreen");
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd("MainScreen"); 
	}
}
